package models

import (
	"go-admin/global/orm"
	"time"
	_ "time"
)

type ZxSign struct {
	Id         int       `json:"id" gorm:"type:int(11) unsigned;primary_key"`      //
	SignerId   int       `json:"signerId,omitempty" gorm:"type:int(11) unsigned;"` // 签约者id
	SignState  string    `json:"signState,omitempty"`
	FinishDate time.Time `json:"finishDate,omitempty" gorm:"type:timestamp;"` // 完成日期
	Note       string    `json:"note" gorm:"type:varchar(200);"`              // 备注
	CreateBy   string    `json:"createBy" gorm:"type:varchar(128);"`          // 创建者
	UpdateBy   string    `json:"updateBy" gorm:"type:varchar(128);"`          // 更新者
	DataScope  string    `json:"dataScope" gorm:"-"`
	Params     string    `json:"params"  gorm:"-"`
	BaseModel
}

func (ZxSign) TableName() string {
	return "zx_sign"
}

// 创建ZxSign
func (e *ZxSign) Create() (ZxSign, error) {
	var doc ZxSign
	result := orm.Eloquent.Table(e.TableName()).Create(&e)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *e
	return doc, nil
}

// 获取ZxSign
func (e *ZxSign) Get() (ZxSign, error) {
	var doc ZxSign
	table := orm.Eloquent.Table(e.TableName())

	if e.Id != 0 {
		table = table.Where("id = ?", e.Id)
	}

	//if e.Signer != "" {
	//	table = table.Where("signer like ?", "%"+e.Signer+"%")
	//}

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

//func (e ZxSign) GetIdByName() (int, error) {
//	if tools.NullStr(e.Signer) {
//		return 0, nil
//	}
//	table := orm.Eloquent.Table(e.TableName())
//
//	if tools.NotNullStr(e.Signer) {
//		table = table.Where("signer = ?", e.Signer)
//	}
//
//	if err := table.First(&e).Error; err != nil {
//		return e.Id, err
//	}
//	return e.Id, nil
//}

// 更新ZxSign
func (e *ZxSign) Update() (update ZxSign, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id = ?", e.Id).First(&update).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}

// 删除ZxSign
func (e *ZxSign) Delete(id int) (success bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id = ?", id).Delete(&ZxSign{}).Error; err != nil {
		success = false
		return
	}
	success = true
	return
}

//批量删除
func (e *ZxSign) BatchDelete(id []int) (Result bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id in (?)", id).Delete(&ZxSign{}).Error; err != nil {
		return
	}
	Result = true
	return
}
